﻿using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Menu;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using Extensions;
using ITaskManager.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Utility.Grid
{
    public class GridMenu
    {
        #region Members
        private GridManager gridManager;

        private GridViewMenu customMenu;
        private List<EventHandler> customEvents = new List<EventHandler>();

        private GridViewMenu customColumnMenu;
        private List<EventHandler> customColumnEvents = new List<EventHandler>();


        private DXSubMenuItem miDuzenle = new DXSubMenuItem("Edit");
        private DXSubMenuItem sm = new DXSubMenuItem();
        private bool isReadOnly = false;

        public GridViewMenu RowMenu;

        public GridViewMenu ColumnMenu;

        public GridManager GridManager
        {
            get { return gridManager; }
            set { gridManager = value; }
        }

        public GridView GridView
        {
            get { return GridManager.GridView; }
        }

        public GridUtility Utility
        {
            get { return GridManager.gridUtility; }

        }

        public bool IsReadOnly
        {
            get
            {
                return isReadOnly;
            }
            set
            {
                isReadOnly = value;
                CreateRowMenu();
            }
        }
        #endregion

        #region Constructor
        public GridMenu(GridManager gridManager)
        {
            GridManager = gridManager;

            customMenu = new GridViewMenu(this.GridView);
            customColumnMenu = new GridViewMenu(this.GridView);
            miDuzenle = new DXSubMenuItem("Edit");

            CreateRowMenu();
            CreateColumnMenu();
        }
        #endregion


        private string currentColumnName = "";
        public string CurrentColumnName
        {
            get { return currentColumnName; }
            set
            {
                currentColumnName = value;
                CreateColumnMenu();
            }
        }

        private string currentBandName = "";
        public string CurrentBandName
        {
            get { return currentBandName; }
            set
            {
                currentBandName = value;
                CreateColumnMenu();
            }
        }



        public void CreateColumnMenu()
        {
            ColumnMenu = new GridViewMenu(this.GridView);
            if (CurrentColumnName != "" &&
                CurrentColumnName != "DX$CheckboxSelectorColumn")
            {
                DXSubMenuItem holder = new DXSubMenuItem("Dondur");
                holder.BeginGroup = true;
                DXMenuCheckItem chk = new DXMenuCheckItem();
                chk.Checked = (GridView.Columns[CurrentColumnName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);
                chk.Caption = "Bu sütunu";
                //chk.Caption = CurrentColumnName + ":" + chk.Checked.ToString();
                chk.CheckedChanged += chkDondur_CheckedChanged;
                holder.Items.Add(chk);

                chk = new DXMenuCheckItem();
                chk.Caption = "Bu ve solundaki sütunları";
                chk.Checked = (GridView.Columns[CurrentColumnName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);
                chk.CheckedChanged += chk_SoldanDondur;

                holder.Items.Add(chk);
                ColumnMenu.Items.Add(holder);
            }
            else if (CurrentBandName != "")
            {
                DXMenuCheckItem chk = new DXMenuCheckItem();
                chk.BeginGroup = true;
                DevExpress.XtraGrid.Views.BandedGrid.BandedGridView bgv = (DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)GridView;
                chk.Checked = (bgv.Bands[CurrentBandName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);
                chk.Caption = "Bu bandı dondur";
                //chk.Caption = CurrentColumnName + ":" + chk.Checked.ToString();
                chk.CheckedChanged += chkBandiDondur_CheckedChanged;
                ColumnMenu.Items.Add(chk);
            }

            //for (int i = 0; i < customColumnMenu.Items.Count; i++)
            //{
            //    DXMenuItem mi = customColumnMenu.Items[i];
            //    DXSubMenuItem x = null;
            //    if (mi.Caption.Contains("Dondur|"))
            //    {
            //        string[] vals = mi.Caption.Split('|);
            //        mi.Caption =  vals[2];
            //        x = new DXSubMenuItem("Dondur");
            //        x.BeginGroup = true;
            //        x.Items.Add(mi);
            //    }
            //    mi.Click -= customColumnEvents[i];
            //    mi.Click += customColumnEvents[i];
            //    if (x != null)
            //        ColumnMenu.Items.Add(x);
            //    else
            //        ColumnMenu.Items.Add(mi);
            //}

            //DXMenuItem menuItem = new DXMenuItem("Sütun Başlıklarını Ayarla", new EventHandler(SutunBasliklariniAyarla_Click));
            //ColumnMenu.Items.Add(menuItem);

            DXMenuItem menuItemMRG = new DXMenuItem("Hücre Birleştir/Ayır", new EventHandler(AllowCellMerge_Click), Resources.MergeTableCells_16x16);
            //menuItemMRG.BeginGroup = true;
            ColumnMenu.Items.Add(menuItemMRG);

            DXSubMenuItem optionsViewMenuItem = new DXSubMenuItem("Görünüm Özellikleri");
            optionsViewMenuItem.BeginGroup = true;
            ColumnMenu.Items.Add(optionsViewMenuItem);

            DXSubMenuItem optionsBehaviourMenuItem = new DXSubMenuItem("Davranış Özellikleri");
            ColumnMenu.Items.Add(optionsBehaviourMenuItem);

            DXSubMenuItem optionsCustomizationMenuItem = new DXSubMenuItem("Değişiklik Özellikleri");
            ColumnMenu.Items.Add(optionsCustomizationMenuItem);

            DXSubMenuItem optionsDetailMenuItem = new DXSubMenuItem("Detay Özellikleri");
            ColumnMenu.Items.Add(optionsDetailMenuItem);

            DXSubMenuItem optionsNavigationMenuItem = new DXSubMenuItem("Navigasyon Özellikleri");
            ColumnMenu.Items.Add(optionsNavigationMenuItem);

            DXSubMenuItem optionsPrintMenuItem = new DXSubMenuItem("Yazdırma Özellikleri");
            ColumnMenu.Items.Add(optionsPrintMenuItem);

            DXSubMenuItem gonderMenuItem = new DXSubMenuItem("Gönder");
            gonderMenuItem.BeginGroup = true;
            ColumnMenu.Items.Add(gonderMenuItem);


            //DXSubMenuItem optionsNewItemRowMenuItem = new DXSubMenuItem("Yeni Kayıt Satırı");
            //optionsViewMenuItem.Items.Add(optionsNewItemRowMenuItem);
            ///*ik
            // if (((RolNesne)e.Menu.View.Tag) != null) optionsNewItemRowMenuItem.Enabled = ((RolNesne)e.Menu.View.Tag).Ekler;
            //*/
            //DXMenuCheckItem menuCheckItem = new DXMenuCheckItem("Yok", (view.OptionsView.NewItemRowPosition == NewItemRowPosition.None), null, new EventHandler(NewItemRowNone_Click));
            //optionsNewItemRowMenuItem.Items.Add(menuCheckItem);

            //menuCheckItem = new DXMenuCheckItem("Üstte", (view.OptionsView.NewItemRowPosition == NewItemRowPosition.Top), null, new EventHandler(NewItemRowTop_Click));
            //optionsNewItemRowMenuItem.Items.Add(menuCheckItem);

            //menuCheckItem = new DXMenuCheckItem("Altta", (view.OptionsView.NewItemRowPosition == NewItemRowPosition.Bottom), null, new EventHandler(NewItemRowBottom_Click));
            //optionsNewItemRowMenuItem.Items.Add(menuCheckItem);

            optionsViewMenuItem.Items.Add(new DXMenuCheckItem("Otomatik Filtreleme Satırı", GridView.OptionsView.ShowAutoFilterRow, null, new EventHandler(AutoFilterRow_Click)));
            optionsViewMenuItem.Items.Add(new DXMenuCheckItem("Dipnot Alanı", GridView.OptionsView.ShowFooter, null, new EventHandler(ShowFooter_Click)));
            //optionsViewMenuItem.Items.Add(new DXMenuCheckItem("Hücre Birleştirme", GridView.OptionsView.AllowCellMerge, null, new EventHandler(AllowCellMerge_Click)));

            DXMenuCheckItem menuCheckItem = new DXMenuCheckItem("Otomatik Kolon Genişliği", GridView.OptionsView.ColumnAutoWidth, null, new EventHandler(ColumnAutoWidth_Click));
            menuCheckItem.BeginGroup = true;
            optionsViewMenuItem.Items.Add(menuCheckItem);

            optionsViewMenuItem.Items.Add(new DXMenuCheckItem("Sütun Başlıkları", GridView.OptionsView.ShowColumnHeaders, null, new EventHandler(ShowColumnHeaders_Click)));
            optionsViewMenuItem.Items.Add(new DXMenuCheckItem("Detay Butonları", GridView.OptionsView.ShowDetailButtons, null, new EventHandler(ShowDetailButtons_Click)));

            DXSubMenuItem subMenuItem = new DXSubMenuItem("Filtreleme Paneli Modu");
            optionsViewMenuItem.Items.Add(subMenuItem);
            /*ik
             * if ((((RolNesne)e.Menu.View.Tag) != null) && (e.Menu.View.ParentView != null))
                subMenuItem.Enabled = (ProgramKullanıcı.AktifKullanıcı.NesneFiltre((RolNesne)e.Menu.View.Tag) == null);
            */
            menuCheckItem = new DXMenuCheckItem("Normal", (GridView.OptionsView.ShowFilterPanelMode == ShowFilterPanelMode.Default), null, new EventHandler(ShowFilterPanelModeDefault_Click));
            subMenuItem.Items.Add(menuCheckItem);

            menuCheckItem = new DXMenuCheckItem("Daima", (GridView.OptionsView.ShowFilterPanelMode == ShowFilterPanelMode.ShowAlways), null, new EventHandler(ShowFilterPanelModeShowAlways_Click));
            subMenuItem.Items.Add(menuCheckItem);

            menuCheckItem = new DXMenuCheckItem("Asla", (GridView.OptionsView.ShowFilterPanelMode == ShowFilterPanelMode.Never), null, new EventHandler(ShowFilterPanelModeNever_Click));
            subMenuItem.Items.Add(menuCheckItem);

            optionsViewMenuItem.Items.Add(new DXMenuCheckItem("Gruplanan Sütunları Göster", GridView.OptionsView.ShowGroupedColumns, null, new EventHandler(ShowGroupedColumns_Click)));
            optionsViewMenuItem.Items.Add(new DXMenuCheckItem("İmleci Göster", GridView.OptionsView.ShowIndicator, null, new EventHandler(ShowIndicator_Click)));

            menuCheckItem = new DXMenuCheckItem("Önİzleme", GridView.OptionsView.ShowPreview, null, new EventHandler(ShowPreview_Click));
            menuCheckItem.BeginGroup = true;
            optionsViewMenuItem.Items.Add(menuCheckItem);

            optionsViewMenuItem.Items.Add(new DXMenuCheckItem("Önizleme Çizgileri", GridView.OptionsView.ShowPreviewRowLines.ToBool(), null, new EventHandler(ShowPreviewLines_Click)));

            menuCheckItem = new DXMenuCheckItem("Satır Çizgileri", GridView.OptionsView.ShowHorizontalLines.ToBool(), null, new EventHandler(ShowHorzLines_Click));
            menuCheckItem.BeginGroup = true;
            optionsViewMenuItem.Items.Add(menuCheckItem);

            optionsViewMenuItem.Items.Add(new DXMenuCheckItem("Sütun Çizgileri", GridView.OptionsView.ShowVerticalLines.ToBool(), null, new EventHandler(ShowVertLines_Click)));

            optionsBehaviourMenuItem.Items.Add(new DXMenuCheckItem("Ardışık Arama", GridView.OptionsBehavior.AllowIncrementalSearch, null, new EventHandler(AllowIncrementalSearch_Click)));
            optionsBehaviourMenuItem.Items.Add(new DXMenuCheckItem("Scrollda Kısmi Satır", GridView.OptionsBehavior.AllowPartialRedrawOnScrolling, null, new EventHandler(AllowPartialRedrawOnScrolling_Click)));
            optionsBehaviourMenuItem.Items.Add(new DXMenuCheckItem("Tüm Grupları Otomatik Aç", GridView.OptionsBehavior.AutoExpandAllGroups, null, new EventHandler(AutoExpandAllGroups_Click)));
            optionsBehaviourMenuItem.Items.Add(new DXMenuCheckItem("Alan Editte Tüm Değerleri Seç", GridView.OptionsBehavior.AutoSelectAllInEditor, null, new EventHandler(AutoSelectAllInEditor_Click)));
            optionsBehaviourMenuItem.Items.Add(new DXMenuCheckItem("Sütun başlıkları ile kopyala", GridView.OptionsBehavior.CopyToClipboardWithColumnHeaders, null, new EventHandler(CopyToClipboardWithColumnHeaders_Click)));

            menuCheckItem = new DXMenuCheckItem("Çoklu Seçme", GridView.OptionsSelection.MultiSelect, null, new EventHandler(MultiSelect_Click));
            menuCheckItem.BeginGroup = true;
            optionsBehaviourMenuItem.Items.Add(menuCheckItem);

            subMenuItem = new DXSubMenuItem("Çoklu Seçme Modu");
            optionsBehaviourMenuItem.Items.Add(subMenuItem);

            menuCheckItem = new DXMenuCheckItem("Satır", (GridView.OptionsSelection.MultiSelectMode == GridMultiSelectMode.RowSelect), null, new EventHandler(MultiSelectRow_Click));
            subMenuItem.Items.Add(menuCheckItem);

            menuCheckItem = new DXMenuCheckItem("Hücre", (GridView.OptionsSelection.MultiSelectMode == GridMultiSelectMode.CellSelect), null, new EventHandler(MultiSelectCell_Click));
            subMenuItem.Items.Add(menuCheckItem);

            optionsCustomizationMenuItem.Items.Add(new DXMenuCheckItem("Sütun Kaydırma", GridView.OptionsCustomization.AllowColumnMoving, null, new EventHandler(AllowColumnMoving_Click)));
            optionsCustomizationMenuItem.Items.Add(new DXMenuCheckItem("Sütun Genişletme", GridView.OptionsCustomization.AllowColumnResizing, null, new EventHandler(AllowColumnResizing_Click)));
            optionsCustomizationMenuItem.Items.Add(new DXMenuCheckItem("Satır Genişletme", GridView.OptionsCustomization.AllowRowSizing, null, new EventHandler(AllowRowSizing_Click)));

            menuCheckItem = new DXMenuCheckItem("Filtreleme", GridView.OptionsCustomization.AllowFilter, null, new EventHandler(AllowFilter_Click));
            /*ik
             * if ((((RolNesne)e.Menu.View.Tag) != null) && (e.Menu.View.ParentView != null))
                menuCheckItem.Enabled = (ProgramKullanıcı.AktifKullanıcı.NesneFiltre((RolNesne)e.Menu.View.Tag) == null);
            */
            menuCheckItem.BeginGroup = true;
            optionsBehaviourMenuItem.Items.Add(menuCheckItem);
            optionsCustomizationMenuItem.Items.Add(new DXMenuCheckItem("Gruplama", GridView.OptionsCustomization.AllowGroup, null, new EventHandler(AllowGroup_Click)));
            optionsCustomizationMenuItem.Items.Add(new DXMenuCheckItem("Sıralama", GridView.OptionsCustomization.AllowSort, null, new EventHandler(AllowSort_Click)));

            optionsDetailMenuItem.Items.Add(new DXMenuCheckItem("Boş Detayları Aç", GridView.OptionsDetail.AllowExpandEmptyDetails, null, new EventHandler(AllowExpandEmptyDetails_Click)));
            optionsDetailMenuItem.Items.Add(new DXMenuCheckItem("Aynı Anda Tek Master Satırı Açılabilsin", GridView.OptionsDetail.AllowOnlyOneMasterRowExpanded, null, new EventHandler(AllowOnlyOneMasterRowExpanded_Click)));
            optionsDetailMenuItem.Items.Add(new DXMenuCheckItem("Detay Odaklaması Olsun", GridView.OptionsDetail.AllowZoomDetail, null, new EventHandler(AllowZoomDetail_Click)));
            optionsDetailMenuItem.Items.Add(new DXMenuCheckItem("Otomatik Detay Odaklaması", GridView.OptionsDetail.AutoZoomDetail, null, new EventHandler(AutoZoomDetail_Click)));
            optionsDetailMenuItem.Items.Add(new DXMenuCheckItem("Detay İpuçlarını Göster", GridView.OptionsDetail.EnableDetailToolTip, null, new EventHandler(EnableDetailToolTip_Click)));
            optionsDetailMenuItem.Items.Add(new DXMenuCheckItem("Master Kayıtlar Açılabilsin", GridView.OptionsDetail.EnableMasterViewMode, null, new EventHandler(EnableMasterViewMode_Click)));
            optionsDetailMenuItem.Items.Add(new DXMenuCheckItem("Detay Tab Başlıklarını Göster", GridView.OptionsDetail.ShowDetailTabs, null, new EventHandler(ShowDetailTabs_Click)));

            optionsNavigationMenuItem.Items.Add(new DXMenuCheckItem("Yeni Kayda Otomatik Geç", GridView.OptionsNavigation.AutoFocusNewRow, null, new EventHandler(AutoFocusNewRow_Click)));
            optionsNavigationMenuItem.Items.Add(new DXMenuCheckItem("Enter Tuşu Sonraki Sütuna Geçirsin", GridView.OptionsNavigation.EnterMoveNextColumn, null, new EventHandler(EnterMoveNextColumn_Click)));
            optionsNavigationMenuItem.Items.Add(new DXMenuCheckItem("Tab Tuşu Sonraki Sütuna Geçirsin", GridView.OptionsNavigation.UseTabKey, null, new EventHandler(UseTabKey_Click)));
            optionsNavigationMenuItem.Items.Add(new DXMenuCheckItem("Tab Tuşu Diğer Kontrole Geçirsin", GridView.OptionsBehavior.FocusLeaveOnTab, null, new EventHandler(FocusLeaveOnTab_Click)));

            optionsPrintMenuItem.Items.Add(new DXMenuCheckItem("Otomatik Genişlik", GridView.OptionsPrint.AutoWidth, null, new EventHandler(AutoWidth_Click)));
            optionsPrintMenuItem.Items.Add(new DXMenuCheckItem("Detayları Yazdır", GridView.OptionsPrint.PrintDetails, null, new EventHandler(PrintDetails_Click)));
            optionsPrintMenuItem.Items.Add(new DXMenuCheckItem("Filtre Bilgisini Yazdır", GridView.OptionsPrint.PrintFilterInfo, null, new EventHandler(PrintFilterInfo_Click)));
            optionsPrintMenuItem.Items.Add(new DXMenuCheckItem("Dipnot Yazdır", GridView.OptionsPrint.PrintFooter, null, new EventHandler(PrintFooter_Click)));
            optionsPrintMenuItem.Items.Add(new DXMenuCheckItem("Grup Dipnot Alanını Yazdır", GridView.OptionsPrint.PrintGroupFooter, null, new EventHandler(PrintGroupFooter_Click)));
            optionsPrintMenuItem.Items.Add(new DXMenuCheckItem("Başlık Alanını Yazdır", GridView.OptionsPrint.PrintHeader, null, new EventHandler(PrintHeader_Click)));
            optionsPrintMenuItem.Items.Add(new DXMenuCheckItem("Ön İzleme Alanını Yazdır", GridView.OptionsPrint.PrintPreview, null, new EventHandler(PrintPreview_Click)));
            optionsPrintMenuItem.Items.Add(new DXMenuCheckItem("Tüm Detayları Aç", GridView.OptionsPrint.ExpandAllDetails, null, new EventHandler(ExpandAllDetails_Click)));
            optionsPrintMenuItem.Items.Add(new DXMenuCheckItem("Tüm Grupları Aç", GridView.OptionsPrint.ExpandAllGroups, null, new EventHandler(ExpandAllGroups_Click)));
            optionsPrintMenuItem.Items.Add(new DXMenuCheckItem("Satır Çizgilerini Yazdır", GridView.OptionsPrint.PrintHorzLines, null, new EventHandler(PrintHorzLines_Click)));
            optionsPrintMenuItem.Items.Add(new DXMenuCheckItem("Sütun Çizgilerini Yazdır", GridView.OptionsPrint.PrintVertLines, null, new EventHandler(PrintVertLines_Click)));

            gonderMenuItem.Items.Add(new DXMenuItem("Excel", new EventHandler(exportToExcel)));
            gonderMenuItem.Items.Add(new DXMenuItem("RTF", new EventHandler(exportToRTF)));
            gonderMenuItem.Items.Add(new DXMenuItem("PDF", new EventHandler(exportToPDF)));
        }

        private void chk_SadeceDoluSutunlariGoster(object sender, EventArgs e)
        {
            DXMenuCheckItem chk = (DXMenuCheckItem)sender;
            Utility.SadeceDoluSutunlariGoster(chk.Checked);
        }

        void chkBandiDondur_CheckedChanged(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Views.BandedGrid.BandedGridView bgv = (DevExpress.XtraGrid.Views.BandedGrid.BandedGridView)GridView;
            bool isfixed = (bgv.Bands[CurrentBandName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);

            if (isfixed)
                bgv.Bands[CurrentBandName].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
            else
                bgv.Bands[CurrentBandName].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;

            CreateColumnMenu();
        }

        void chk_SoldanDondur(object sender, EventArgs e)
        {
            int visibleIndex = GridView.Columns[CurrentColumnName].VisibleIndex;
            bool isfixed = (GridView.Columns[CurrentColumnName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);
            List<DevExpress.XtraGrid.Columns.GridColumn> cols = new List<DevExpress.XtraGrid.Columns.GridColumn>();
            for (int i = 0; i < visibleIndex + 1; i++)
            {
                cols.Add(GridView.VisibleColumns[i]);
            }
            foreach (var o in cols)
            {
                if (isfixed)
                    o.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
                else
                    o.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            }

            CreateColumnMenu();
        }



        void chkDondur_CheckedChanged(object sender, EventArgs e)
        {
            bool isfixed = (GridView.Columns[CurrentColumnName].Fixed == DevExpress.XtraGrid.Columns.FixedStyle.Left);
            if (isfixed)
                GridView.Columns[CurrentColumnName].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
            else
                GridView.Columns[CurrentColumnName].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;

            CreateColumnMenu();

        }

        public void CreateRowMenu()
        {
            RowMenu = new GridViewMenu(this.GridView);
            miDuzenle = new DXSubMenuItem("Edit");
            if (customMenu.Items.Count > 0)
            {
                for (int i = 0; i < customMenu.Items.Count; i++)
                {
                    DXMenuItem mi = customMenu.Items[i];
                    mi.Click -= customEvents[i];
                    mi.Click += customEvents[i];
                    RowMenu.Items.Add(mi);
                }

                int index = RowMenu.Items.Add(miDuzenle);
                RowMenu.Items[index].BeginGroup = true;
            }
            AddStandartMenuItems();
        }

        public void AddMenu(string caption, EventHandler click, bool beginGroup = false)
        {
            DXMenuItem di = new DXMenuItem(caption);
            customEvents.Add(click);
            customMenu.Items.Add(di);
            di.BeginGroup = beginGroup;
            CreateRowMenu();
        }

        public void AddColumnMenu(string caption, EventHandler click)
        {
            DXMenuItem di = new DXMenuItem(caption);
            customColumnEvents.Add(click);
            customColumnMenu.Items.Add(di);

            CreateColumnMenu();
        }

        public void AddColumnMenu(string caption, string Base, EventHandler click)
        {

            DXMenuItem di = new DXMenuItem(caption);
            customColumnEvents.Add(click);
            customColumnMenu.Items.Add(di);

            CreateColumnMenu();
        }


        public void AddMenu(string caption, EventHandler click, Image image)
        {
            DXMenuItem di = new DXMenuItem(caption);
            di.Image = image;
            customEvents.Add(click);
            customMenu.Items.Add(di);

            CreateRowMenu();
        }

        public void AddMenu(DevExpress.XtraBars.BarButtonItem bbi)
        {
            DXMenuItem di = new DXMenuItem(bbi.Caption);
            di.Image = bbi.Glyph;
            EventHandler ev = new EventHandler(delegate(object o, EventArgs e) { bbi.PerformClick(); });
            customEvents.Add(ev);
            customMenu.Items.Add(di);

            CreateRowMenu();
        }

        public void AddMenu(string caption, EventHandler click, Image image, bool beginGroup)
        {
            DXMenuItem di = new DXMenuItem(caption);
            di.Image = image;
            customEvents.Add(click);
            customMenu.Items.Add(di);
            di.BeginGroup = beginGroup;
            CreateRowMenu();
        }

        public void RemoveColumnMenu(string caption)
        {
            for (int i = 0; i < customColumnMenu.Items.Count; i++)
            {
                DXMenuItem mi = customColumnMenu.Items[i];
                if (mi.Caption == caption)
                {
                    customColumnMenu.Items.RemoveAt(i);
                    customColumnEvents.RemoveAt(i);
                    CreateColumnMenu();
                    break;
                }
            }
        }


        public void RemoveMenu(string caption)
        {
            for (int i = 0; i < customMenu.Items.Count; i++)
            {
                DXMenuItem mi = customMenu.Items[i];
                if (mi.Caption == caption)
                {
                    customMenu.Items.RemoveAt(i);
                    customEvents.RemoveAt(i);
                    CreateRowMenu();
                    break;
                }
            }
        }




        private void addToMenu(DXMenuItem menuItem)
        {
            if (customMenu.Items.Count > 0)
            {
                miDuzenle.Items.Add(menuItem);
            }
            else
            {
                RowMenu.Items.Add(menuItem);
            }

        }

        private void addToMenu(DXSubMenuItem subMenu)
        {
            if (customMenu.Items.Count > 0)
            {
                miDuzenle.Items.Add(subMenu);
            }
            else
            {
                RowMenu.Items.Add(subMenu);
            }
        }

        /// <summary>
        /// Does not exist yet!
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public void AddSubMenu(string caption)
        {
        }

        private void AddStandartMenuItems()
        {

            DXMenuItem menuItem;
            menuItem = new DXMenuItem("Kopyala", new EventHandler(copy), ITaskManager.Properties.Resources.CopyHS);
            menuItem.Shortcut = Shortcut.CtrlC;
            addToMenu(menuItem);

            menuItem = new DXMenuItem("Başlıklarla Kopyala", new EventHandler(copyWithHeaders)/*, Properties.Resources.TableHS*/);
            addToMenu(menuItem);

            if (!GridUtility.IsReadOnly(GridView) & !IsReadOnly)
            {
                menuItem = new DXMenuItem("Yapıştır", new EventHandler(paste), ITaskManager.Properties.Resources.PasteHS);
                menuItem.Shortcut = Shortcut.CtrlV;
                addToMenu(menuItem);

                menuItem = new DXMenuItem("Seçili Hücreleri Satırlara Yapıştır", new EventHandler(satirYapistir)/*, Properties.Resources.TableHS*/);
                addToMenu(menuItem);

                menuItem = new DXMenuItem("Sil", new EventHandler(delete), ITaskManager.Properties.Resources.DeleteHS);
                menuItem.Shortcut = Shortcut.Del;
                menuItem.BeginGroup = true;
                addToMenu(menuItem);

                if (!GridManager.RowDeleteDisabled)
                {
                    menuItem = new DXMenuItem("Satır Sil", new EventHandler(deleteRow));
                    menuItem.Shortcut = Shortcut.CtrlDel;
                    addToMenu(menuItem);
                }
            }

            menuItem = new DXMenuItem("Tümünü Seç", new EventHandler(selectAll));
            menuItem.Shortcut = Shortcut.CtrlA;
            //menuItem.BeginGroup = true;
            addToMenu(menuItem);

            menuItem = new DXMenuItem("Sütuna Git", new EventHandler(sutunaGit));
            menuItem.Shortcut = Shortcut.CtrlG;
            menuItem.BeginGroup = true;
            addToMenu(menuItem);

            DXMenuCheckItem chk = new DXMenuCheckItem();
            chk.Caption = "Sadece Dolu Sütunları Göster";

            if (GridManager.ShowOnlyNonblankColumns)
                chk.Checked = true;
            chk.CheckedChanged += chk_SadeceDoluSutunlariGoster;
            addToMenu(chk);

            if (!GridUtility.IsReadOnly(GridView) & !IsReadOnly)
            {
                menuItem = new DXMenuItem("Toplu Giriş", new EventHandler(bulkInsert), ITaskManager.Properties.Resources.Categorized);
                menuItem.Shortcut = Shortcut.CtrlT;
                addToMenu(menuItem);
            }
            DXSubMenuItem subMenu = new DXSubMenuItem("Gönder");
            addToMenu(subMenu);

            menuItem = new DXMenuItem("Excel", new EventHandler(exportToExcel), ITaskManager.Properties.Resources.XLS);
            subMenu.Items.Add(menuItem);

            menuItem = new DXMenuItem("PDF", new EventHandler(exportToPDF), ITaskManager.Properties.Resources.PDF);
            subMenu.Items.Add(menuItem);

            menuItem = new DXMenuItem("Yazdır", new EventHandler(Yazdir_Click), ITaskManager.Properties.Resources.PrintHS);
            addToMenu(menuItem);

            //menuItem = new DXMenuCheckItem("Hücre Birleştirme", GridView.OptionsView.AllowCellMerge, null, new EventHandler(AllowCellMerge_Click));
            //addToMenu(menuItem);
        }




        #region Menu Functions
        //private void SutunBasliklariniAyarla_Click(object sender, EventArgs e)
        //{
        //    ArrayList sutunBaslikArrayList = new ArrayList();
        //    for (int i = 0; i < GridView.VisibleColumns.Count; i++)
        //    {
        //        sutunBaslikArrayList.Add(new SutunBaslik(GridView.VisibleColumns[i].VisibleIndex + 1, GridView.VisibleColumns[i].Caption, GridView.VisibleColumns[i].Caption));
        //    }
        //    //SutunBasliklariForm sutunBasliklari = new SutunBasliklariForm();
        //    //sutunBasliklari.columnGridControl.DataSource = sutunBaslikArrayList;
        //    //if (sutunBasliklari.ShowDialog() == DialogResult.OK)
        //    {
        //        foreach (SutunBaslik sutun in sutunBaslikArrayList)
        //        {
        //            GridView.VisibleColumns[sutun.ID - 1].Caption = sutun.YeniBaslik;
        //        }
        //    }
        //}

        private void NewItemRowNone_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
        }

        private void NewItemRowTop_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.NewItemRowPosition = NewItemRowPosition.Top;
        }

        private void NewItemRowBottom_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
        }

        private void AutoFilterRow_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowAutoFilterRow = !GridView.OptionsView.ShowAutoFilterRow;
        }

        private void ShowFooter_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowFooter = !GridView.OptionsView.ShowFooter;
        }

        private void AllowCellMerge_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.AllowCellMerge = !GridView.OptionsView.AllowCellMerge;
        }

        private void Yazdir_Click(object sender, EventArgs e)
        {
            // Check whether the XtraGrid control can be previewed. 
            if (!GridView.GridControl.IsPrintingAvailable)
            {
                XtraMessageBox.Show("The 'DevExpress.XtraPrinting' Library is not found", "Error");
                return;
            }
            // Opens the Preview window. 
            GridView.ShowPrintPreview();
        }

        private void ColumnAutoWidth_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ColumnAutoWidth = !GridView.OptionsView.ColumnAutoWidth;
        }

        private void ShowColumnHeaders_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowColumnHeaders = !GridView.OptionsView.ShowColumnHeaders;
        }

        private void ShowDetailButtons_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowDetailButtons = !GridView.OptionsView.ShowDetailButtons;
        }

        private void ShowFilterPanelModeDefault_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Default;
        }

        private void ShowFilterPanelModeShowAlways_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.ShowAlways;
        }

        private void ShowFilterPanelModeNever_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
        }

        private void ShowGroupedColumns_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowGroupedColumns = !GridView.OptionsView.ShowGroupedColumns;
        }

        private void ShowIndicator_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowIndicator = !GridView.OptionsView.ShowIndicator;
        }

        private void ShowPreview_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowPreview = !GridView.OptionsView.ShowPreview;
        }

        private void ShowPreviewLines_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowPreviewRowLines = GridView.OptionsView.ShowPreviewRowLines.Toggle();
        }

        private void ShowHorzLines_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowHorizontalLines = GridView.OptionsView.ShowHorizontalLines.Toggle();
        }

        private void ShowVertLines_Click(object sender, EventArgs e)
        {
            GridView.OptionsView.ShowVerticalLines = GridView.OptionsView.ShowVerticalLines.Toggle();
        }

        private void AllowIncrementalSearch_Click(object sender, EventArgs e)
        {
            GridView.OptionsBehavior.AllowIncrementalSearch = !GridView.OptionsBehavior.AllowIncrementalSearch;
        }

        private void CopyToClipboardWithColumnHeaders_Click(object sender, EventArgs e)
        {
            //GridView.OptionsBehavior.CopyToClipboardWithColumnHeaders = !GridView.OptionsBehavior.CopyToClipboardWithColumnHeaders;
            GridView.OptionsClipboard.CopyColumnHeaders = (GridView.OptionsClipboard.CopyColumnHeaders == DevExpress.Utils.DefaultBoolean.False ? DevExpress.Utils.DefaultBoolean.True : DevExpress.Utils.DefaultBoolean.False);

        }

        private void AllowPartialRedrawOnScrolling_Click(object sender, EventArgs e)
        {
            GridView.OptionsBehavior.AllowPartialRedrawOnScrolling = !GridView.OptionsBehavior.AllowPartialRedrawOnScrolling;
        }

        private void AutoExpandAllGroups_Click(object sender, EventArgs e)
        {
            GridView.OptionsBehavior.AutoExpandAllGroups = !GridView.OptionsBehavior.AutoExpandAllGroups;
        }

        private void AutoSelectAllInEditor_Click(object sender, EventArgs e)
        {
            GridView.OptionsBehavior.AutoSelectAllInEditor = !GridView.OptionsBehavior.AutoSelectAllInEditor;
        }

        private void MultiSelect_Click(object sender, EventArgs e)
        {
            GridView.OptionsSelection.MultiSelect = !GridView.OptionsSelection.MultiSelect;
        }

        private void MultiSelectRow_Click(object sender, EventArgs e)
        {
            GridView.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
        }

        private void MultiSelectCell_Click(object sender, EventArgs e)
        {
            GridView.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CellSelect;
        }

        private void AllowColumnMoving_Click(object sender, EventArgs e)
        {
            GridView.OptionsCustomization.AllowColumnMoving = !GridView.OptionsCustomization.AllowColumnMoving;
        }

        private void AllowColumnResizing_Click(object sender, EventArgs e)
        {
            GridView.OptionsCustomization.AllowColumnResizing = !GridView.OptionsCustomization.AllowColumnResizing;
        }

        private void AllowRowSizing_Click(object sender, EventArgs e)
        {
            GridView.OptionsCustomization.AllowRowSizing = !GridView.OptionsCustomization.AllowRowSizing;
        }

        private void AllowFilter_Click(object sender, EventArgs e)
        {
            GridView.OptionsCustomization.AllowFilter = !GridView.OptionsCustomization.AllowFilter;
        }

        private void AllowGroup_Click(object sender, EventArgs e)
        {
            GridView.OptionsCustomization.AllowGroup = !GridView.OptionsCustomization.AllowGroup;
        }

        private void AllowSort_Click(object sender, EventArgs e)
        {
            GridView.OptionsCustomization.AllowSort = !GridView.OptionsCustomization.AllowSort;
        }

        private void AllowExpandEmptyDetails_Click(object sender, EventArgs e)
        {
            GridView.OptionsDetail.AllowExpandEmptyDetails = !GridView.OptionsDetail.AllowExpandEmptyDetails;
        }

        private void AllowOnlyOneMasterRowExpanded_Click(object sender, EventArgs e)
        {
            GridView.OptionsDetail.AllowOnlyOneMasterRowExpanded = !GridView.OptionsDetail.AllowOnlyOneMasterRowExpanded;
        }

        private void AllowZoomDetail_Click(object sender, EventArgs e)
        {
            GridView.OptionsDetail.AllowZoomDetail = !GridView.OptionsDetail.AllowZoomDetail;
        }

        private void AutoZoomDetail_Click(object sender, EventArgs e)
        {
            GridView.OptionsDetail.AutoZoomDetail = !GridView.OptionsDetail.AutoZoomDetail;
        }

        private void EnableDetailToolTip_Click(object sender, EventArgs e)
        {
            GridView.OptionsDetail.EnableDetailToolTip = !GridView.OptionsDetail.EnableDetailToolTip;
        }

        private void EnableMasterViewMode_Click(object sender, EventArgs e)
        {
            GridView.OptionsDetail.EnableMasterViewMode = !GridView.OptionsDetail.EnableMasterViewMode;
        }

        private void ShowDetailTabs_Click(object sender, EventArgs e)
        {
            GridView.OptionsDetail.ShowDetailTabs = !GridView.OptionsDetail.ShowDetailTabs;
        }

        private void AutoFocusNewRow_Click(object sender, EventArgs e)
        {
            GridView.OptionsNavigation.AutoFocusNewRow = !GridView.OptionsNavigation.AutoFocusNewRow;
        }

        private void EnterMoveNextColumn_Click(object sender, EventArgs e)
        {
            GridView.OptionsNavigation.EnterMoveNextColumn = !GridView.OptionsNavigation.EnterMoveNextColumn;
        }

        private void UseTabKey_Click(object sender, EventArgs e)
        {
            GridView.OptionsNavigation.UseTabKey = !GridView.OptionsNavigation.UseTabKey;
        }

        private void FocusLeaveOnTab_Click(object sender, EventArgs e)
        {
            GridView.OptionsBehavior.FocusLeaveOnTab = !GridView.OptionsBehavior.FocusLeaveOnTab;
        }

        private void AutoWidth_Click(object sender, EventArgs e)
        {
            GridView.OptionsPrint.AutoWidth = !GridView.OptionsPrint.AutoWidth;
        }

        private void PrintDetails_Click(object sender, EventArgs e)
        {
            GridView.OptionsPrint.PrintDetails = !GridView.OptionsPrint.PrintDetails;
        }

        private void PrintFilterInfo_Click(object sender, EventArgs e)
        {
            GridView.OptionsPrint.PrintFilterInfo = !GridView.OptionsPrint.PrintFilterInfo;
        }

        private void PrintFooter_Click(object sender, EventArgs e)
        {
            GridView.OptionsPrint.PrintFooter = !GridView.OptionsPrint.PrintFooter;
        }

        private void PrintGroupFooter_Click(object sender, EventArgs e)
        {
            GridView.OptionsPrint.PrintGroupFooter = !GridView.OptionsPrint.PrintFooter;
        }

        private void PrintHeader_Click(object sender, EventArgs e)
        {
            GridView.OptionsPrint.PrintHeader = !GridView.OptionsPrint.PrintHeader;
        }

        private void PrintPreview_Click(object sender, EventArgs e)
        {
            GridView.OptionsPrint.PrintPreview = !GridView.OptionsPrint.PrintPreview;
        }

        private void ExpandAllDetails_Click(object sender, EventArgs e)
        {
            GridView.OptionsPrint.ExpandAllDetails = !GridView.OptionsPrint.ExpandAllDetails;
        }

        private void ExpandAllGroups_Click(object sender, EventArgs e)
        {
            GridView.OptionsPrint.ExpandAllGroups = !GridView.OptionsPrint.ExpandAllGroups;
        }

        private void PrintHorzLines_Click(object sender, EventArgs e)
        {
            GridView.OptionsPrint.PrintHorzLines = !GridView.OptionsPrint.PrintHorzLines;
        }

        private void PrintVertLines_Click(object sender, EventArgs e)
        {
            GridView.OptionsPrint.PrintVertLines = !GridView.OptionsPrint.PrintVertLines;
        }
        #endregion

        #region Menu Item Event Handlers
        private void selectAll(object sender, EventArgs e)
        {
            Utility.SelectAll();
        }

        private void sutunaGit(object sender, EventArgs e)
        {
            Utility.SutunaGit();
        }

        private void copy(object sender, EventArgs e)
        {
            Utility.Copy();
        }

        public void copyWithHeaders(object sender, EventArgs e)
        {
            Utility.CopyWithHeaders();
        }

        private void paste(object sender, EventArgs e)
        {
            Utility.PasteFromClipboard();
        }

        private void delete(object sender, EventArgs e)
        {
            Utility.DeleteValue();
        }

        private void deleteRow(object sender, EventArgs e)
        {
            Utility.DeleteRow();
        }

        private void exportToExcel(object sender, EventArgs e)
        {
            Utility.Export("xlsx");
        }

        private void exportToRTF(object sender, EventArgs e)
        {
            Utility.Export("rtf");
        }

        private void exportToPDF(object sender, EventArgs e)
        {
            Utility.Export("pdf");
        }

        private void bulkInsert(object sender, EventArgs e)
        {
            GridManager.FireBulkInsert();
        }

        private void satirYapistir(object sender, EventArgs e)
        {
            Utility.SatirYapistir();
        }
        #endregion






        internal void AddMenu(string p)
        {
            throw new NotImplementedException();
        }
    }
}